<?php
/*
* Copyright (c) 2010 by Cediti
*
* You may distribute under the terms of either the GNU Lesser General
* Public License or the new BSD License, as specified in the README file.
*/

// created :	30 April 2010
// version :	30 April 2010

class Cmis_Client_Services_Alfresco_Discovery extends Cmis_Client_Services_Generic_Discovery
{
	/**
	 * Return a set of properties
	 * 
	 * @see https://issues.alfresco.com/jira/browse/ALF-2649 => return partial entry so we need to solved this before anything
	 * @see Cmis_Client_Services_Generic_Discovery#query()
	 * @return array[Cmis_Repository_Properties]
	 */
	public function query(Cmis_Repository_Id $repositoryId, $statement, $searchAllVersions = false, $includeRelationships = null, $renditionFilter = null, $includeAllowableActions = false, $maxItems = null, $skipCount = null)
    {
    	//same as parent - start -----------------------------------------------
        $cur = $this->_getRepositoryInfo($repositoryId);

        switch ($cur->getCapabilities('Query'))
        {
            default:
            case Cmis_Repository_Capability_Query::VALUE_NONE :
                throw new Cmis_Exception_Services_Services_Unavailable('query');
                break;
            case Cmis_Repository_Capability_Query::VALUE_METADATAONLY :
                if (stristr($string, 'CONTAINS('))
                {
                    throw new Cmis_Exception_Services_Services_Unavailable('query with contains');
                }
                break;
            case Cmis_Repository_Capability_Query::VALUE_BOTHCOMBINED :
            case Cmis_Repository_Capability_Query::VALUE_BOTHSEPARATE :
            case Cmis_Repository_Capability_Query::VALUE_FULLTEXTONLY :
                //can check so ...ok :(
                break;
        }

        if (stristr($string, ' JOIN '))
        {
            switch ($cur->getCapapbilities('Join'))
            {
                default:
                case Cmis_Repository_Capability_Join::VALUE_NONE :
                    throw new Cmis_Exception_Services_Services_Unavailable('join');
                    break;
                case Cmis_Repository_Capability_Join::VALUE_INNERANDOUTER :
                    break;
                case Cmis_Repository_Capability_Join::VALUE_INNERONLY :
                    if (preg_match('#(LEFT|OUTER) JOIN#si', $statement))
                    {
                        throw new Cmis_Exception_Services_Services_Unavailable('join with outer or left');
                    }
                    break;
            }
        }

        $response = $this->getServices()->getProtocolService('Discovery')->query($repositoryId, $statement, $searchAllVersions, $includeRelationships, $renditionFilter, $includeAllowableActions, $maxItems, $skipCount);
        if (empty($response))
        {
            return null;
        }

        $ret = array();
        $xml = $response->toXml();

        //same as parent - end -------------------------------------------------
        $prefix = (func_num_args() > 8)? func_get_arg(9):'';

        $results = array();
        $entries = (in_array('atom', $xml->getDocNamespaces()))?$xml->xpath('//atom:entry'):$xml->xpath('//entry');

        foreach($entries as $entry)
        {
        	$properties = new Cmis_Repository_Properties();
        	$properties->setXmlResponse($entry, $prefix);
        	$results[] = $properties;
        }

        $properties = new Cmis_Repository_Properties();
        $properties->setXmlResponse($xml, $prefix);

        return $results;
    }
}

# EOF
